home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Bitmap.C < prev    next >
C/C++ Source or Header  |  1992-08-26  |  5KB  |  303 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "Bitmap.h"
  6.  
  7. #include "Class.h"
  8. #include "DevBitmap.h"
  9. #include "Error.h"
  10. #include "Storage.h"
  11. #include "WindowSystem.h"
  12. #include "String.h"
  13. #include "OrdColl.h"
  14. #include "Data.h"
  15.  
  16. #include "STREAMFILTERS/HexFilter.h"
  17. #include "STREAMFILTERS/LZWFilter.h"
  18. #include "STREAMFILTERS/RLEFilter.h"
  19. #include "STREAMFILTERS/A85Filter.h"
  20.  
  21. //---- SmartBitmap -------------------------------------------------------------
  22.  
  23. Bitmap *SmartBitmap::MakeBitmap()
  24. {
  25.     if (name) {
  26.     FileData *fd;
  27.     if (name[0] == '/')
  28.         fd= new FileData((char*)name);
  29.     else {
  30.         fd= new FileData(form("/%s/src/images/%s", gEtDir, name));
  31.     }
  32.     bm= (Bitmap*) fd->AsObject(Meta(Bitmap));
  33.     delete fd;
  34.     } else if (bits) {
  35.     bm= new Bitmap(size, bits);
  36.     } else
  37.     fprintf(stderr, "SmartBitmap::MakeBitmap(): oops\n");
  38.     return bm;
  39. }
  40.  
  41. //---- Bitmap ------------------------------------------------------------------
  42.  
  43. NewMetaImpl0(Bitmap,Ink);
  44.  
  45. Bitmap::Bitmap(Point sz, u_short depth)
  46. {
  47.     WindowSystem::WSInit();
  48.     dbm= gWindowSystem->MakeDevBitmap(sz, 0, depth);
  49.     mask= 0;
  50.     if (dbm)
  51.     dbm->Ref();
  52. }
  53.  
  54. Bitmap::Bitmap(Point sz, u_short *im, u_short depth)
  55. {    
  56.     WindowSystem::WSInit();
  57.     dbm= gWindowSystem->MakeDevBitmap(sz, im, depth);
  58.     mask= 0;
  59.     if (dbm)
  60.     dbm->Ref();
  61. }
  62.  
  63. Bitmap::Bitmap(int sz, u_short *im, u_short depth)
  64. {    
  65.     WindowSystem::WSInit();
  66.     dbm= gWindowSystem->MakeDevBitmap(sz, im, depth);
  67.     mask= 0;
  68.     if (dbm)
  69.     dbm->Ref();
  70. }
  71.  
  72. Bitmap::Bitmap(DevBitmap *d)
  73. {    
  74.     WindowSystem::WSInit();
  75.     dbm= d;
  76.     mask= 0;
  77.     if (dbm)
  78.     dbm->Ref();
  79. }
  80.  
  81. Bitmap::Bitmap(const char*)
  82. {    
  83.     WindowSystem::WSInit();
  84.     fprintf(stderr, "Bitmap::Bitmap((char*): not yet implemented\n");    
  85.     dbm= mask= 0;
  86.     if (dbm)
  87.     dbm->Ref();
  88. }
  89.  
  90. Bitmap::~Bitmap()
  91. {
  92.     if (dbm) {
  93.     dbm->Unref();
  94.     dbm= 0;
  95.     }
  96.     if (mask) {
  97.     mask->Unref();
  98.     mask= 0;
  99.     }
  100. }
  101.  
  102. Point Bitmap::Size()
  103. {
  104.     return dbm->Size();
  105. }
  106.  
  107. int Bitmap::ShortsPerLine()
  108. {
  109.     return ((dbm->Size().x-1)/16+1)*dbm->Depth();
  110. }
  111.  
  112. int Bitmap::BytesPerLine()
  113. {
  114.     return dbm->BytesPerLine();
  115. }
  116.  
  117. int Bitmap::GetDepth()
  118. {
  119.     return dbm->Depth();
  120. }
  121.  
  122. void Bitmap::SetDevBitmap(DevBitmap *d)
  123. {
  124.     if (dbm) {
  125.     dbm->Unref();
  126.     dbm= 0;
  127.     }
  128.     if (d) {
  129.     dbm= d;
  130.     dbm->Ref();
  131.     }
  132.     Changed();
  133. }
  134.  
  135. void Bitmap::SetDevMask(DevBitmap *m)
  136. {
  137.     if (mask) {
  138.     mask->Unref();
  139.     mask= 0;
  140.     }
  141.     if (m) {
  142.     mask= m;
  143.     mask->Ref();
  144.     }
  145.     Changed();
  146. }
  147.  
  148. void Bitmap::Fill(u_int val)
  149. {
  150.     Point sz= Size();
  151.     for (int x= 0; x < sz.x; x++)
  152.     for (int y= 0; y < sz.y; y++)
  153.         dbm->SetPixel(x, y, val);
  154.     Changed();
  155. }
  156.        
  157. void Bitmap::seedfill(int x, int y, u_int seed, u_int val)
  158. {
  159.     if (GetPixel(x, y) == seed) {
  160.     dbm->SetPixel(x, y, val);
  161.     seedfill(x-1, y, seed, val);
  162.     seedfill(x+1, y, seed, val);
  163.     seedfill(x, y+1, seed, val);
  164.     seedfill(x, y-1, seed, val);
  165.     }
  166. }
  167.  
  168. void Bitmap::SeedFill(Point where, u_int val)
  169. {
  170.     u_int oldval= GetPixel(where.x, where.y);
  171.     if (val == oldval)    // prevent endless recursion
  172.     seedfill(where.x, where.y, oldval, oldval= (val+1) % 256);
  173.     seedfill(where.x, where.y, oldval, val);
  174.     Changed();
  175. }
  176.  
  177. void Bitmap::SetInk(Port *port)
  178. {
  179.     port->DevSetPattern(dbm);
  180. }
  181.  
  182. void Bitmap::SetPixel(u_int x, u_int y, u_int val)
  183. {
  184.     if (x >= 0 && x < dbm->Size().x && y >= 0 && y < dbm->Size().y) {
  185.     dbm->SetPixel(x, y, val);
  186.     Rectangle r(x, y, 1, 1);
  187.     Send(cIdNone, cPartPixelChanged, &r);
  188.     }
  189. }
  190.  
  191. u_int Bitmap::GetPixel(u_int x, u_int y)
  192. {
  193.     if (x >= 0 && x < dbm->Size().x && y >= 0 && y < dbm->Size().y)
  194.     return dbm->GetPixel(x, y);
  195.     return 0;
  196. }
  197.  
  198. Object *Bitmap::deepclone()
  199. {
  200.     if (IsA() == Meta(Bitmap)) {
  201.     Bitmap *bm= new Bitmap(Size(), GetDepth());
  202.     bm->dbm= dbm->DeepClone();
  203.     return bm;
  204.     }
  205.     return 0;
  206. }
  207.  
  208. OStream &Bitmap::PrintOn(OStream &s)
  209. {
  210.     register int x, y;
  211.     int enc;
  212.     Point size= Size();
  213.     int cmsize= dbm->GetColormapSize();
  214.     int bpl= BytesPerLine();
  215.     
  216.     Object::PrintOn(s);
  217.     
  218.     enc= 0;
  219.     if (Bytes() > 100) {
  220.     if (GetDepth() > 1)
  221.         enc= 3;
  222.     else 
  223.         enc= 2;
  224.     }
  225.     
  226.     s << size SP << GetDepth() SP << enc SP << cmsize NL;
  227.     
  228.     if (cmsize > 0)
  229.     s << *dbm->cmap NL;
  230.     
  231.     if (enc == 0) {
  232.     s.Push(new HexEncoder);
  233.     } else if (enc == 1) {
  234.     s.Push(new HexEncoder);
  235.     s.Push(new RLEEncoder);
  236.     } else if (enc == 2) {
  237.     s.Push(new A85Encoder);
  238.     s.Push(new RLEEncoder);
  239.     } else if (enc == 3) {
  240.     s.Push(new A85Encoder);
  241.     s.Push(new LZWEncoder);
  242.     }
  243.     
  244.     register StreamBuf *sb= s.getsbuf();
  245.     for (y= 0; y < size.y; y++)
  246.     for (x= 0; x < bpl; x++) {
  247.         int c= (int) dbm->GetByte(x, y);
  248.         sb->sputc(c);
  249.     }
  250.     
  251.     if (enc > 0)
  252.     s.Pop();
  253.  
  254.     s.Pop();
  255.     return s;
  256. }
  257.  
  258. IStream &Bitmap::ReadFrom(IStream &s)
  259. {
  260.     int decd, depth, cmsize;
  261.     Point size;
  262.     
  263.     if (dbm) {
  264.     dbm->Unref();
  265.     dbm= 0;
  266.     }
  267.     
  268.     Object::ReadFrom(s);
  269.     
  270.     s >> size >> depth >> decd >> cmsize;
  271.     
  272.     dbm= gWindowSystem->MakeDevBitmap(size, 0, depth);
  273.     if (dbm)
  274.     dbm->Ref();
  275.     
  276.     if (cmsize > 0) {
  277.     dbm->SetColormapSize(cmsize);
  278.     s >> *dbm->cmap;
  279.     }
  280.  
  281.     if (decd == 0) {
  282.     s.Push(new HexDecoder);
  283.     } else if (decd == 1) {
  284.     s.Push(new HexDecoder);
  285.     s.Push(new RLEDecoder(TRUE));
  286.     } else if (decd == 2) {
  287.     s.Push(new A85Decoder);
  288.     s.Push(new RLEDecoder(FALSE));
  289.     } else if (decd == 3) {
  290.     s.Push(new A85Decoder);
  291.     s.Push(new LZWDecoder);
  292.     }
  293.     
  294.     dbm->ReadData(s, depth, 1);
  295.  
  296.     if (decd > 0)
  297.     s.Pop();
  298.     
  299.     s.Pop();
  300.     return s;
  301. }
  302.  
  303.